package com.logic.modular.game.player.info;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.logic.comment.util.RedisUtils;
import com.logic.config.netty.web.service.MessageService;
import com.comment.pojo.domain.logic.domain.PlayerStatus;
import com.logic.modular.game.player.info.mapper.PlayerStatusMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * 基础服务-玩家状态
 *
 * @author cxy
 * @since 2024-09-12 19:49:54
 */
@Slf4j
@Service
public class PlayerStatusBaseServiceImpl extends ServiceImpl<PlayerStatusMapper, PlayerStatus> {
    private static final String cacheKey = "player:%d:status";
    @Resource
    private RedisTemplate<Object, Object> redisTemplate;
    @Resource
    private ValueOperations<Object, PlayerStatus> valueOperations;
    @Resource
    private PlayerStatusMapper playerMatchMapper;
    @Resource
    private RedisUtils redisUtils;


    ///////////////////////////////////////////////////////////////////////////////////////////////////////////  基础查询
    public PlayerStatus get(Long playerId) {
        String key = String.format(cacheKey, playerId);
        PlayerStatus playerMatch = valueOperations.get(key);
        if (playerMatch == null) {
            playerMatch = playerMatchMapper.selectById(playerId);
            if (playerMatch == null) {
                playerMatch = PlayerStatus.builder()
                        .uid(playerId)
                        .currentMatchId(0L)
                        .roomOwnerId(0L)
                        .build();
                playerMatch.insert();
            }
            valueOperations.set(key, playerMatch, 1, TimeUnit.DAYS);
            redisTemplate.expire(key, 60, TimeUnit.MINUTES);
        }
        return playerMatch;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////  更新插入
    @Override
    public boolean updateById(PlayerStatus entity) {
        boolean b = super.updateById(entity);
        if (b) {
            PlayerStatus byId = super.getById(entity.getUid());
            savaCache(byId);
            MessageService.sendMessageToUser(entity.getUid(), MessageService.updateJsonObject("playerStatus", entity));
        }
        return b;
    }

    @Override
    public boolean save(PlayerStatus entity) {
        boolean save = super.save(entity);
        entity = super.getById(entity.getUid());
        savaCache(entity);
        return save;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////  缓存部分
    public void savaCache(PlayerStatus entity) {
        String key1 = String.format(cacheKey, entity.getUid());
        valueOperations.set(key1, entity, 60, TimeUnit.MINUTES);
    }

    public PlayerStatus selectBy(
            @NotNull(message = "playerId不能为空") Long playerId
    ) {
        String key1 = String.format(cacheKey, playerId);
        redisTemplate.expire(key1, 60, TimeUnit.MINUTES);
        redisUtils.expire(key1, 60, TimeUnit.MINUTES);
        PlayerStatus playerStatus = (PlayerStatus) redisUtils.get(key1);
        if (playerStatus == null) {
            playerStatus = getById(playerId);
            redisUtils.set(key1, playerStatus, 60, TimeUnit.MINUTES);
        }
        return playerStatus;
    }

    public void removeCache(PlayerStatus entity) {
        String key1 = String.format(cacheKey, entity.getUid());
        redisTemplate.delete(key1);
    }
}
